package com.guguskins.back.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

@Slf4j
public class QixintongUtil {

    public static void main(String[] args) throws UnsupportedEncodingException {

//        String encode = java.net.URLEncoder.encode("https://x.imgtu.com/i/Ufq9a",   "utf-8");
        //13210012890杜春苗
//        String state = sendMobileMessageByURL("13361216473","您购买的星巴克中杯兑换券为请在有效期内使用，过期作废，有效期请查看公众号订单详情【惠多多】",logger,1);
        String state = sendMobileMessageByURL("15864544659", "测试验证码：888888【guguskins】", 0);

        System.out.println(state);
    }

    /**
     * 通过URL地址的形式发送短信
     *
     * @param mobileNum
     * @param content
     * @return
     */
    public static String sendMobileMessageByURL(String mobileNum, String content, int detection) {
        /*if (detection == 1) {
            ChuanglanUtil chuanglanUtil = new ChuanglanUtil();
            boolean chuanglanResult = chuanglanUtil.chuanglan(mobileNum);
            if (chuanglanResult == false) {
                return "手机号码检测为空号";
            }
        }*/

        String state = "0";
        try {
            URL url = new URL("http://115.29.103.223:8080/smsServer/submit");//短信URL地址
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            log.info("调用第三方发送短信==>手机号：" + mobileNum + "==>短信内容：" + content);
            String mobileMessageInfo = "CORPID=qingdaoyahe&CPPW=" + MessageMD5.MD5("qingdaoyahe") + "&PHONE=" + mobileNum + "&CONTENT=" + content;
            out.write(mobileMessageInfo);
            out.flush();
            out.close();
            DataInputStream in = new DataInputStream(connection.getInputStream());
            state = in.readLine();//1为短信发送成功，否则短信发送失败
            log.info("调用第三方发送短信==>返回信息：" + state);
            in.close();
        } catch (IOException e) {
            log.error("发送短信失败", e);
        }
        return state;
    }

}
